package com.yungu.swift.order.processor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.DriverDto;
import com.yungu.swift.assets.driver.model.dto.DriverMessageDto;
import com.yungu.swift.assets.driver.service.DriverMessageService;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.common.model.dto.CommonMessageStatusDto;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.socket.push.model.PushContent;
import com.yungu.swift.socket.push.service.PushService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;


/**
 * 推送处理器
 *
 * @author Zerrion
 * @date 2017/5/8
 */
@Component
public class PushProcessor implements PayProcessor {
    private static final Logger logger = LoggerFactory.getLogger(PushProcessor.class);
    @Reference
    PushService pushService;
    @Reference
    private DriverService driverService;
    @Reference
    private DriverMessageService driverMessageService;
    @Reference
    private CommonMessageStatusService commonMessageStatusService;

    @Override
    public void process(PayVo payVo) {
        if (payVo.getPaySide() == CommonConstant.PASSENGER || payVo.getPaySide() == CommonConstant.PASSENGER_PULBIC || payVo.getPaySide() == CommonConstant.PASSENGER_H5) {
            //推司机
            String msg = "收到乘客尾号" + StringUtils.right(payVo.getPassengerVo().getMobile(), 4) + "支付" + payVo.getOrderBalance().toString() + "元，可到我的钱包内查看";
            Map<String, Object> pushDriverMap = MapUtils.build();
            pushDriverMap.put("orderUuid", payVo.getOrderUuid());
            pushDriverMap.put("title", "收到支付信息");
            pushDriverMap.put("content", msg);
            pushDriverMap.put("report", msg);
            pushDriverMap.put("typeModule", payVo.getOrderDto().getTypeModule());
            PushContent pushDriverContent = PushContent.newInstance4DriWithJPush(payVo.getAppid(), PushContent
                    .ORDER_PASSENGER_ORDER_PAYED, pushDriverMap);
            pushService.sendMessage(payVo.getDriverVo().getUuid(), pushDriverContent);
            //插入系统消息到 t_common_driver_message 和 t_common_message_status表
            CompletableFuture.runAsync(() -> savaDriverMessage(payVo, msg));
        }
        if (payVo.getPaySide() == CommonConstant.DRIVER) {
            //推乘客
            Map<String, Object> pushPassengerMap = MapUtils.build();
            pushPassengerMap.put("orderUuid", payVo.getOrderUuid());
            pushPassengerMap.put("title", "收到支付信息");
            pushPassengerMap.put("content", "司机已代付");
            pushPassengerMap.put("typeModule", payVo.getOrderDto().getTypeModule());
            PushContent pushPassengerContent = PushContent.newInstance4Pas(payVo.getAppid(),
                    PushContent.ORDER_DRIVER_HELP_PAYED_SUCCESS, pushPassengerMap);
            pushService.sendMessage(payVo.getPassengerVo().getUuid(), pushPassengerContent);
        }
        logger.info(payVo.getDescription() + "--处理推送逻辑完毕");
    }

    private void savaDriverMessage(PayVo payVo, String msg) {
        DriverMessageDto commonDriverMessageDto = new DriverMessageDto();
        commonDriverMessageDto.setUuid(StringUtils.buildUUID());
        //支付消息
        commonDriverMessageDto.setType(3);
        //业务类型
        commonDriverMessageDto.setBusinessType(payVo.getDriverVo().getBusinessType());
        //文本
        commonDriverMessageDto.setSendType(1);
        commonDriverMessageDto.setTitle("支付信息");
        commonDriverMessageDto.setContent(msg);
        //指定对象
        commonDriverMessageDto.setSendObject(2);
        commonDriverMessageDto.setTargetUuid(payVo.getDriverVo().getUuid());
        commonDriverMessageDto.setAppid(payVo.getAppid());
        commonDriverMessageDto.setSendTime(new Date());

        commonDriverMessageDto.setCreateOn(new Date());
        commonDriverMessageDto.setCreateBy(payVo.getOrderDto().getActualDriverUuid());
        commonDriverMessageDto.setUpdateBy(payVo.getOrderDto().getActualDriverUuid());
        commonDriverMessageDto.setAppid(payVo.getAppid());
        DriverDto driverDto = driverService.get(payVo.getDriverVo().getUuid()).getData();
        if (driverDto != null) {
            commonDriverMessageDto.setTargetName(driverDto.getName());
            commonDriverMessageDto.setTargetDriverMobile(driverDto.getMobile());
            commonDriverMessageDto.setAgentUuid(driverDto.getAgentUuid());
            commonDriverMessageDto.setCompanyUuid(driverDto.getCompanyUuid());
        }
        ResponseData<Boolean> ajaxList = driverMessageService.add(commonDriverMessageDto);
        if (ajaxList.isSuccess() && Objects.equals(ajaxList.getData(), Boolean.TRUE)) {
            logger.info("订单：{}，支付消息插入成功", payVo.getOrderUuid());
            CommonMessageStatusDto commonMessageStatusDto = new CommonMessageStatusDto();
            commonMessageStatusDto.setUuid(StringUtils.buildUUID());
            commonMessageStatusDto.setMessageUuid(commonDriverMessageDto.getUuid());
            commonMessageStatusDto.setTargetUuid(payVo.getDriverVo().getUuid());
            //支付消息
            commonMessageStatusDto.setType(3);
            commonMessageStatusDto.setBusinessType(payVo.getDriverVo().getBusinessType());
            //未读
            commonMessageStatusDto.setStatus(1);
            //司机
            commonMessageStatusDto.setTargetType(CommonConstant.DRIVER);
            commonMessageStatusDto.setAppid(payVo.getAppid());
            commonMessageStatusService.add(commonMessageStatusDto);
            logger.info("订单：{}，支付消息状态成功", payVo.getOrderUuid());
            return;
        }
    }
}
